﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TVESTAMM 
    {
     /// <summary>
     /// 查询出所有结果，执行一系列操做
     /// </summary>
        public TVESTAMM()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVESTAMM(); 
            this.DataContext = MV;
            InitializeComponent(); 
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["VEMANDANT"] = app.Mandant;
            dwData["VEVERTNR"] = "";
            dwData["VEVERTNA"] =" AND VEVERTNA LIKE '"+ART.Trim()+"%'";
            dwData["VENAME1"] = " AND VENAME1 LIKE '" + DES.Trim() + "%'";
            SResult = ((VVESTAMM)MV).QueryVESTAMM(dwData, 2);
            MergeData(SResult, true);
        }
   

        
        public void Insert(DataRow _dwVS)
        {
            try
            { 
                ((VVESTAMM)MV).DeleteVESTAMM(_dwVS, 1);
                ((VVESTAMM)MV).InsertVESTAMM(_dwVS, app.company);
                TBMain.Rows.InsertAt(_dwVS, 0);
               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
         

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                ERTNA = "";
                return;
            }
            if (ERTNA == "")
                return;
            dwData = TBMain.NewRow();
            ((VVESTAMM)MV).SetUpNewRow(ref dwData);
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "VESTAMM");
            dwData["VEVERTNR"] = Helpers.Helper.Right("00000000" + LFD, 8);
            dwData["VEVERTNA"] = ERTNA;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst(); 
            IsNew = false;
            ERTNA = "";
            base.NewCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;         
            ((VVESTAMM)MV).UpdateVESTAMM(dwMaster, app.company, 0);
            base.SaveCommand(sender, e);
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
           
            filter = "VEVERTNR='" + dwMaster["VEVERTNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                ((VVESTAMM)MV).DeleteVESTAMM(row, 1);
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                ERTNA = "";
                return;
            }
            if (ERTNA == "")
                return;
            DataRow dwData = TBMain.NewRow();
            dwData.ItemArray = dwVS.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "VESTAMM");
            dwData["VEVERTNR"] = Helpers.Helper.Right("00000000" + LFD, 8);
            dwData["VEVERTNA"] = ERTNA;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            ERTNA = "";
            base.CopyCommand(sender, e);
        }
        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["VEMANDANT"] = app.Mandant;
            dwData["VEVERTNR"] = " AND VEVERTNR='"+ORDERNUM+"'";
            SResult = ((VVESTAMM)MV).QueryVESTAMM(dwData, 2);
            DataTable temp = TBMain.Clone();
            temp.Merge(SResult.Tables[0]);
            foreach (DataRow row in temp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }  
        }
    }
}
